package tcp

import "lnzw/engine/net/marshal"

type ITcpSocket interface {
	Encoder(encoder marshal.IEncode) ITcpSocket
	Decoder(decoder marshal.IDecode) ITcpSocket
	Encode(data []byte) ([]byte, error)
	Decode(data []byte) ([]byte, int, error)
	OnConnect(client *SocketClient)
	OnMessage(client *SocketClient, data []byte)
	OnClose(client *SocketClient)
	Start()
}

type TcpSocket struct {
	encoder      marshal.IEncode
	decoder      marshal.IDecode
	socketHandle SocketHandle
}

func (s *TcpSocket) Encoder(encoder marshal.IEncode) ITcpSocket {
	s.encoder = encoder
	return s
}

func (s *TcpSocket) Decoder(decoder marshal.IDecode) ITcpSocket {
	s.decoder = decoder
	return s
}

func (s *TcpSocket) BindSocketHandle(socketHandle SocketHandle) ITcpSocket {
	s.socketHandle = socketHandle
	return s
}

func (s *TcpSocket) Encode(data []byte) ([]byte, error) {
	return s.encoder.Encode(data)
}
func (s *TcpSocket) Decode(data []byte) ([]byte, int, error) {
	return s.decoder.Decode(data)
}

func (s *TcpSocket) OnConnect(client *SocketClient) {
	s.socketHandle.OnConnect(client)
}
func (s *TcpSocket) OnMessage(client *SocketClient, data []byte) {
	s.socketHandle.OnMessage(client, data)
}
func (s *TcpSocket) OnClose(client *SocketClient) {
	s.socketHandle.OnClose(client)
}

func (s *TcpSocket) Start() {
	panic("please implement me")
}
